﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Diagnostics;

using AMPLibrary;
using AMPLibrary.Controllers;
using AMPLibrary.Media;
using AMPLibrary.Windows;

namespace MediaLibrary
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static MainWindow mainWindow;

        private MusicPage musicPage = MusicPage.GetMusicPage();

        private NowPlayingPage nowPlayingPage = NowPlayingPage.GetNowPlayingPage();

        private VideoPage videoPage = new VideoPage();

        private PlaylistPage playlistPage = new PlaylistPage();

        private SettingsPage settingsPage = new SettingsPage();

        private SearchPage searchPage = new SearchPage();

        private PersonalPage personalPage = PersonalPage.GetPersonalPage();

        private ArtistInformationPage artistInformationPage = ArtistInformationPage.GetArtistInformationPage();

        private MediaCentrePage mediaCentrePage = new MediaCentrePage();

        private WatchFolderController watchFolderController = WatchFolderController.GetWatchFolderController();
        
        /// <summary>
        /// 
        /// </summary>
        private bool IsDragging
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        private MainWindow()
        {
            InitializeComponent();

            SourceInitialized += delegate { HwndSource source = (HwndSource)PresentationSource.FromVisual(this); source.AddHook(ScreenSaver.DisableScreenSaverHook); };

            // setup the application
            this.WindowStartupLocation = WindowStartupLocation.Manual;
            this.Width = Settings.ApplicationWidth;
            this.Height = Settings.ApplicationHeight;

            this.searchTextBox.Text = "search";
            this.searchTextBox.Foreground = Brushes.Gray;

            SetPage(Pages.NowPlaying);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static MainWindow GetMainWindow()
        {
            if (mainWindow == null)
                mainWindow = new MainWindow();

            return mainWindow;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="commandLineArguements"></param>
        public void HandleCommandLineArgs(IList<string> commandLineArguements)
        {
            if (commandLineArguements.Count > 1)
            {
                if (MediaTypes.IsValidMediaType(commandLineArguements[1]))
                {
                    string path = commandLineArguements[1];
                    AMPLibrary.Media.MediaPlayer mediaPlayer = AMPLibrary.Media.MediaPlayer.GetMediaPlayer();

                    if (MediaTypes.IsValidMusicMediaType(path) || MediaTypes.IsValidVideoMediaType(path))
                    {
                        mediaPlayer.NowPlayingCollection.AddItem(MediaFile.LoadMedia(path));
                        MediaFile mediaToPlay = MediaFile.LoadMedia(path);
                        mediaPlayer.Play(ref mediaToPlay);
                    }
                    else if (MediaTypes.IsValidPlaylistMediaType(path))
                        mediaPlayer.NowPlayingCollection.AddItems(Playlist.LoadPlaylist(path));
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
        }

        /// <summary>
        /// Remove the margins on the window
        /// </summary>
        /// <param name="isViewable"></param>
        public void ToggleMargin(bool isViewable)
        {
            if (isViewable == false)
            {
                this.pagePlaceHolder.Margin = new Thickness(0);
                this.pagePlaceHolder.Margin = new Thickness(0);
                this.pagePlaceHolder.Margin = new Thickness(0);
                this.pagePlaceHolder.Margin = new Thickness(0);
            }
            else if (isViewable == true)
            {
                this.pagePlaceHolder.Margin = new Thickness(6);
                this.pagePlaceHolder.Margin = new Thickness(6);
                this.pagePlaceHolder.Margin = new Thickness(6);
                this.pagePlaceHolder.Margin = new Thickness(6);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void ShowMainMenuControls(bool showMainMenu)
        {
            if (showMainMenu)
                LayoutRoot.RowDefinitions[0].Height = new GridLength(35.0);
            else
                LayoutRoot.RowDefinitions[0].Height = new GridLength(0.0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitButton_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            App.Current.Shutdown();

            AMPLibrary.Media.MediaPlayer mediaPlayer = AMPLibrary.Media.MediaPlayer.GetMediaPlayer();
            mediaPlayer.Stop();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void musicButton_Click(object sender, RoutedEventArgs e)
        {
            SetPage(Pages.Music);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoButton_Click(object sender, RoutedEventArgs e)
        {
            SetPage(Pages.Video);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nowPlayingButton_Click(object sender, RoutedEventArgs e)
        {
            SetPage(Pages.NowPlaying);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void settingsButton_Click(object sender, RoutedEventArgs e)
        {
            SetPage(Pages.Settings);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void helpButton_Click(object sender, RoutedEventArgs e)
        {
            SetPage(Pages.Help);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void playListButton_Click(object sender, RoutedEventArgs e)
        {
            SetPage(Pages.Playlist);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deviceButton_Click(object sender, RoutedEventArgs e)
        {
            SetPage(Pages.Device);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mediaServerButton_Click(object sender, RoutedEventArgs e)
        {
            SetPage(Pages.MediaServer);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mediaCentrePageButton_Click(object sender, RoutedEventArgs e)
        {
            SetPage(Pages.MediaCentre);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void personalButton_Click(object sender, RoutedEventArgs e)
        {
            PersonalPage.GetPersonalPage().BindInformation();
            SetPage(Pages.Personal);
        }

        /// <summary>
        /// Event handler, fired when the user changes the state of the window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MainWindow_OnStateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
                Settings.Maximized = true;
            else
                Settings.Maximized = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void minimizeButton_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void maximizeButton_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// Event handler, fired each time the master window changes location
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MainWindow_LocationChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void MainWindow_OnSizeChanged(object sender, EventArgs e)
        {
            Settings.ApplicationWidth = this.Width;
            Settings.ApplicationHeight = this.Height;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        public void SetPage(Pages page)
        {
            this.musicButton.IsChecked = false;
            this.videoButton.IsChecked = false;
            this.playListButton.IsChecked = false;
            this.nowPlayingButton.IsChecked = false;
            this.settingsButton.IsChecked = false;
            this.personalButton.IsChecked = false;
            //this.mediaCentrePageButton.IsChecked = false;

            switch (page)
            {
                case Pages.Settings:
                    this.settingsButton.IsChecked = true;
                    this.pagePlaceHolder.Navigate(settingsPage);
                    break;

                case Pages.Music:
                    this.musicButton.IsChecked = true;
                    this.pagePlaceHolder.Navigate(musicPage);
                    break;

                case Pages.Video:
                    this.videoButton.IsChecked = true;
                    this.pagePlaceHolder.Navigate(videoPage);
                    break;

                case Pages.NowPlaying:
                    this.nowPlayingButton.IsChecked = true;
                    this.pagePlaceHolder.Navigate(nowPlayingPage);
                    break;

                case Pages.Playlist:
                    this.playListButton.IsChecked = true;
                    this.pagePlaceHolder.Navigate(playlistPage);
                    break;

                case Pages.Personal:
                    this.personalPage.BindInformation();
                    this.pagePlaceHolder.Navigate(personalPage);
                    this.personalButton.IsChecked = true;
                    break;

                case Pages.Search:
                    this.pagePlaceHolder.Navigate(searchPage);
                    break;

                case Pages.ArtistInformation:
                    this.pagePlaceHolder.Navigate(artistInformationPage);
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.IsDragging = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.IsDragging = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsDragging && e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.Key == Key.Return)
            {
                if(searchTextBox.Text != string.Empty && this.searchTextBox.Text != "search")
                    Search();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            this.searchTextBox.Text = string.Empty;
            this.searchTextBox.Foreground = Brushes.Black;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            this.searchTextBox.Text = "search";
            this.searchTextBox.Foreground = Brushes.Gray;
        }

        /// <summary>
        /// 
        /// </summary>
        private void Search()
        {
            Pause();

            SetPage(Pages.Search);
            searchPage.Search(searchTextBox.Text);

            Unpause();
        }

        /// <summary>
        /// Method, stops the music folder controller from scanning while the page is reading from the collection
        /// </summary>
        private void Pause()
        {
            watchFolderController.PauseMusic = true;
            watchFolderController.PauseVideo = true;
            watchFolderController.PausePlaylist = true;
            System.Threading.Thread.Sleep(100);
        }

        /// <summary>
        /// Method, tells the music folder controller to resume scanning
        /// </summary>
        private void Unpause()
        {
            watchFolderController.PauseMusic = false;
            watchFolderController.PauseVideo = false;
            watchFolderController.PausePlaylist = false;
        }
    }
}